<?php
namespace Admin\Controller;
use Admin\Model\UserLogModel;

class GoodController extends MainController{

    /**
     * 在售商品列表
     * @queyuan
     * 2016/05/31
     * @param $page,flag(1-最新、2-剩余、3-人气、4-总需),$c_id
     */
    public function sales(){
        $flag = I("param.flag");
        $page = I("param.page",0,'int');
        $c_id = I("param.c_id",0,'int');
        $where = "s.status = 1 and s.code_id = 0 and s.sale_num < s.need_num";
        $result = array();
        if($flag == 1){
            $order = "s.create_time desc";
        }elseif($flag == 2){
            $order = "s.sale_num desc";
        }elseif($flag == 3){
            $order = "left_num asc";
        }elseif($flag == 4){
            $order = "s.need_num desc";
        }else{
            $order = "s.ord desc";
        }
        if($c_id){
            $sons = M("category")->where("pid = ".$c_id." and status = 1")->count();
            if($sons){
                $where .= " and g.p_cat = ".$c_id;
            }else{
                $where .= " and g.sub_cat = ".$c_id;
            }
        }
        $count = M("sales s")->join("left join one_goods g on (s.good_id = g.id)")->where($where)->count();
        $num = ceil($count / C("pagesize"));
        if($page < 0){
            $page = 0;
        }
        if($page > $num){
            $page = $num - 1;
        }
        $limit = C("pagesize") * $page;
        $list = M("sales s")->join("left join one_goods g on (s.good_id = g.id)")->where($where)->order($order)->field("s.id,s.good_id,s.price,s.need_num,s.sale_num,(s.need_num - s.sale_num) as left_num,g.name as good_name,g.index_img as img")->limit($limit,C("pagesize"))->select();
        if($list){
            $result['status'] = 0;
            $result['msg'] = "请求成功";
            $result['list'] = $list;
            $result['page'] = $page;
            $result['num'] = $num;
        }else{
            $result['status'] = 1;
            $result['msg'] = "无数据";
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 商品详情
     * @queuyan
     * 2016/05/31
     * @param $id,$user_id,$good_id
     */
    public function saleDetail(){
        $id = I("param.id");
        $good_id = I("param.good_id");
        $user_id = I("param.user_id");
        $result = array();
        if(!$id && !$good_id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            if(!$id){
                $id = M("sales")->where("good_id = ".$good_id." and code_id = 0")->order("sale_num desc")->getField("id");
            }
            if(!$id){
                $result['status'] = 1;
                $result['msg'] = "该商品已下架";
            }else{
                $detail = M("sales s")->join("left join one_goods g on (s.good_id = g.id)")->where("s.id = ".$id." and s.status = 1")->field("s.id,s.good_id,s.need_num,s.sale_num,s.allow_coupon,(s.need_num - s.sale_num) as left_num,g.name as good_name,g.img,g.intro")->find();
                if(!$detail){
                    $result['status'] = 1;
                    $result['msg'] = "无此在售商品";
                }else{
                    $detail['win'] = M("sales s")->join("left join one_order_code c on (s.code_id = c.id) left join one_order o on (c.order_id = o.id) left join one_user u on (o.user_id = u.id)")->where("s.good_id = ".$detail['good_id']." and s.code_id != 0")->order("s.win_time desc")->field("u.name as username,u.id as user_id,s.win_time,s.id as sale_id,u.avatar,u.avatar_type")->find();
                    if($detail['win']){
                        $detail['win']['count'] = M("order_sales s")->join("left join one_order o on (s.order_id = o.id)")->where("s.sale_id = ".$detail['win']['sale_id']." and o.user_id = ".$detail['win']['user_id'])->getField("sum(s.num)");
                    }
                    $detail['img'] = explode(",",$detail['img']);
                    $detail['percent'] = round($sale_num / $need_num) * 100;
                    $result['status'] = 0;
                    $result['msg'] = "请求成功";
                    $result['detail'] = $detail;
                }
            }
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 首页推荐商品
     * @queyuan
     * 2016/05/30
     * @param $flag(0-最新、1-剩余、2-人气、3-总需)
     */
    public function recommendSales(){
        $flag = I("param.flag");
        $where = "s.status = 1 and s.code_id = 0 and s.is_recommend = 1 and s.sale_num < s.need_num";
        $result = array();
        if($flag == 0){
            $order = "s.create_time desc";
        }elseif($flag == 2){
            $order = "s.sale_num desc";
        }elseif($flag == 1){
            $order = "left_num asc";
        }elseif($flag == 3){
            $order = "s.need_num desc";
        }else{
            $order = "s.ord desc";
        }
        $list = M("sales s")->join("left join one_goods g on (s.good_id = g.id)")->where($where)->order($order)->field("s.id,s.good_id,s.price,s.need_num,s.sale_num,(s.need_num - s.sale_num) as left_num,g.name as good_name,g.index_img as img")->select();
        if($list){
            $result['status'] = 0;
            $result['msg'] = "请求成功";
            $result['list'] = $list;
        }else{
            $result['status'] = 1;
            $result['msg'] = "无数据";
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 获取往期开奖
     * @queyuan
     * 2016/07/15
     * @param $id
     */
    public function getLastWin(){
        $id = I("param.id");
        $result = array();
        if(!$id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $good_id = M("sales")->where("id = ".$id)->getField("good_id");
            $list = M("sales s")->join("left join one_order_code c on (s.code_id = c.id) left join one_order o on (c.order_id = o.id) left join one_user u on (o.user_id = u.id)")->where("s.good_id = ".$good_id." and s.code_id != 0")->field("s.id as sale_id,u.name as user_name,c.code,u.avatar,u.avatar_type,o.user_id")->order("s.win_time desc")->select();
            foreach ($list as &$item) {
                $item['count'] = M("order_sales s")->join("left join one_order o on (s.order_id = o.id)")->where("s.sale_id = ".$item['sale_id']." and o.user_id = ".$item['user_id'])->getField("sum(s.num)");
            }
            $result['status'] = 0;
            $result['msg'] = "请求成功";
            $result['list'] = $list;
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 最新揭晓列表
     * @queyuan
     * 2016/07/15
     * @param 
     */
    public function newList(){
        $result = array();
        $time = time() - (15 * 60);
        $list = M("sales s")->join("left join one_goods g on (s.good_id = g.id)")->where("s.win_time > ".time()." and s.code_id != 0")->order("s.win_time asc")->field("g.index_img,g.name as good_name,s.win_time")->select();
        $result['status'] = 0;
        $result['msg'] = "请求成功";
        $result['list'] = $list;
        echo json_encode($result);
        exit;
    }

    /**
     * 获取参与列表
     * @queyuan
     * 2016/07/16
     * @param $sale_id
     */
    public function joinRecord(){
        $sale_id = I("param.sale_id");
        $result = array();
        if(!$sale_id){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $list = M("order_sales s")->join("left join one_order o on (s.order_id = o.id) left join one_user u on (o.user_id = u.id)")->where("s.sale_id = ".$sale_id." and o.status = 1")->order("o.pay_time desc")->field("u.name as user_name,u.avatar,u.avatar_type,s.num,o.pay_time")->select();
            $result['status'] = 0;
            $result['msg'] = "请求成功";
            $result['list'] = $list;
        }
        echo json_encode($result);
        exit;
    }
}
?>